package im.elephas.androfan.common;

import im.elephas.androfan.fanfouapi.FanfouApi;
import im.elephas.androfan.fanfouapi.events.IChangeEvent;
import im.elephas.androfan.fanfouapi.events.IErrorEvent;
import im.elephas.androfan.fanfouapi.events.IUpdateEvent;
import im.elephas.androfan.fanfouapi.items.StatusItem;

import android.app.Activity;
import android.widget.Toast;

public class ApiCaller {

	private static FanfouApi mFanfouApi = FanfouApi.getInstance();

	private Activity mActivity;

	public IErrorEvent mIErrorEvent = new DefaultIErrorEvent();

	public ApiCaller(Activity mActivity) {
		this.mActivity = mActivity;
	}

	private class DefaultIErrorEvent implements IErrorEvent {

		@Override
		public void errorHandler() {
			mActivity.runOnUiThread(new Runnable() {

				@Override
				public void run() {
					Toast.makeText(mActivity, "请检查网络设置", Toast.LENGTH_SHORT)
							.show();
				}
			});
		}
	}

	private void defaultHandler(final String str) {
		mActivity.runOnUiThread(new Runnable() {

			@Override
			public void run() {
				Toast.makeText(mActivity, str, Toast.LENGTH_SHORT).show();
			}
		});
	}

	public void deleteMyStatus(StatusItem si) {
		IChangeEvent ice = new IChangeEvent() {

			@Override
			public void changeHandler(boolean success) {
				mActivity.runOnUiThread(new Runnable() {

					@Override
					public void run() {
						Toast.makeText(mActivity, "删除成功", Toast.LENGTH_SHORT)
								.show();
					}
				});
			}
		};
		mFanfouApi.destroyStatus(si.getId(), ice, new DefaultIErrorEvent());
	}

	public void createFavoriteStatus(StatusItem si) {
		IChangeEvent ice = new IChangeEvent() {

			@Override
			public void changeHandler(boolean success) {
				if (success)
					defaultHandler("收藏成功");
				else
					defaultHandler("已收藏");
			}
		};
		mFanfouApi.createFavorite(si.getId(), ice, new DefaultIErrorEvent());
	}

	public void destroyFavoriteStatus(StatusItem si) {
		IChangeEvent ice = new IChangeEvent() {

			@Override
			public void changeHandler(boolean success) {
				if (success)
					defaultHandler("删除成功");
				else
					defaultHandler("已删除");
			}
		};
		mFanfouApi.destroyFavorite(si.getId(), ice, new DefaultIErrorEvent());
	}

	public void refanfouStatus(StatusItem si) {
		IUpdateEvent iue = new IUpdateEvent() {

			@Override
			public void updateHandler(String result) {
				defaultHandler("已转发");
			}
		};
		mFanfouApi.refanfouMessage("转@" + si.getUser().getScreenName() + " "
				+ si.getText(), si.getId(), iue, new DefaultIErrorEvent());
	}
	
	public void updateStatus(String str) {
		IUpdateEvent iue = new IUpdateEvent() {

			@Override
			public void updateHandler(String result) {
				defaultHandler("发送成功");
			}
		};
		mFanfouApi.updateMessage(str,iue, new DefaultIErrorEvent());
	}
}
